package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.BspsMchntAcctInfoMapper;
import cc.rengu.igas.bsps.common.entity.MchntAcctInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * .
 */
public class BspsMchntAcctInfoMapperImpl implements BspsMchntAcctInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public MchntAcctInfo selectBspsMchntAcctInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntAcctInfoById");
        MchntAcctInfo mchntAcctInfo = new MchntAcctInfo();
        mchntAcctInfo.setInstId(instId);
        mchntAcctInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntAcctInfo, mchntAcctInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户结算账户信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return mchntAcctInfo;
    }

    @Override
    public MchntAcctInfo selectBspsMchntAcctInfoByPrimaryKey(String instId, String mchntNo, String baseProdCode, String fundType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntAcctInfoByPrimaryKey");
        MchntAcctInfo mchntAcctInfo = new MchntAcctInfo();
        mchntAcctInfo.setInstId(instId);
        mchntAcctInfo.setMchntNo(mchntNo);
        mchntAcctInfo.setBaseProdCode(baseProdCode);
        mchntAcctInfo.setFundType(fundType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntAcctInfo, mchntAcctInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户结算账户信息失败,instId:<{}>,mchntNo:<{}>,baseProdCode:<{}>,fundType:<{}>,iReturnCode:<{}>", instId, mchntNo, baseProdCode, fundType, iReturnCode);
            return null;
        }
        return mchntAcctInfo;
    }

    @Override
    public List<MchntAcctInfo> selectBspsMchntAcctInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntAcctInfoByMchntNo");
        MchntAcctInfo mchntAcctInfo = new MchntAcctInfo();
        mchntAcctInfo.setInstId(instId);
        mchntAcctInfo.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntAcctInfo, MchntAcctInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户结算账户信息失败,instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntAcctInfo) item).collect(Collectors.toList());
    }
}
